using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using Watchdog.Bll;
using Watchdog.Bll.Factory;
using log4net;
using Microsoft.VisualBasic.Devices;

namespace Watchdog {
    class Program {
        static DispatcherClient dispatcher;
        static ILog log = LogManager.GetLogger ( "servicehost.Logging" );
        static Computer MyComputer = new Computer ();

        static void Main ( string[] args ) {
            try {
                Log.Write (Severity.Info, "Starting up");
                cb callb = new cb ();
                dispatcher = new DispatcherClient (new InstanceContext (callb));

                Start ();
                Console.ReadLine ();
                Stop ();
            } catch (System.Exception ex) {
                Log.Write (Severity.Fatal, ex.Message, ex);
            }
        }

        public static void Start () {
            dispatcher.Subscribe ();
            TestUnit me = Watchdog.Bll.Factory.TestUnitFactory.GetByName (MyComputer.Name.ToLower ());
            if (me == TestUnit.Empty) {
                // Testunit does not exist... we create it
                me.Name = MyComputer.Name.ToLower ();
                me.Update ();
            }
            dispatcher.SetOnline (Watchdog.Bll.Factory.TestUnitFactory.GetFromBll (me));
            //dispatcher.SetOnline ( MyComputer.Name );
        }
        public static void Stop () {
            TestUnit me = Watchdog.Bll.Factory.TestUnitFactory.GetByName (MyComputer.Name.ToLower ());
            dispatcher.SetOffline (Watchdog.Bll.Factory.TestUnitFactory.GetFromBll(me));
            dispatcher.Unsubscribe ();
        }
    }
    internal class cb : IDispatcherCallback {

        #region IDispatcherCallback Members

        public void ExecuteSchedule ( Watchdog.Facade.Schedule schedule ) {
            Log.Write ( Severity.Info, "Received Schedule." );

        }

        public void ExecuteTest ( Watchdog.Facade.Test test ) {
            Log.Write ( Severity.Info, "Received Test." );
        }

        #endregion
    }
}


